<template>
  <div
    class="chat-container"
    :style="{
      height: containerHeight,
      width: containerWidth,
      maxHeight: maxHeight,
      maxWidth: maxWidth
    }"
  >
    <!-- 聊天主区域 -->
    <div class="chat-main">
      <!-- 聊天历史区域 -->
      <div class="chat-history">
        <div
          v-for="(message, index) in messages"
          :key="index"
          :class="['message', message.sender === 'user' ? 'user-message' : 'bot-message']">
          <div v-if="message.sender === 'user'" class="avatar user-avatar">
            <a-icon type="user" />
          </div>
          <div v-else class="avatar bot-avatar">
            <a-icon type="robot" />
          </div>

          <div class="message-content">
            <div v-html="message.content"></div>
            <div class="timestamp">{{ message.timestamp }}</div>
          </div>
        </div>

        <div v-if="isLoading" class="message bot-message">
          <div class="avatar bot-avatar">
            <a-icon type="robot" />
          </div>
          <div class="loading-dots">
            <div class="dot"></div>
            <div class="dot"></div>
            <div class="dot"></div>
          </div>
        </div>
      </div>
    </div>

    <!-- 输入区域 -->
    <div class="input-area">
      <a-input
        v-model="inputMessage"
        placeholder="输入您的问题..."
        @pressEnter="sendMessage"
        :disabled="isLoading"
      >
        <a-icon slot="prefix" type="message" />
      </a-input>
      <a-button
        type="primary"
        shape="circle"
        icon="arrow-up"
        class="send-button"
        @click="sendMessage"
        :loading="isLoading"
      />
    </div>
  </div>
</template>

<script>
import { chat } from '@/api/ai/chat'

export default {
  name: 'CompactChat',
  props: {
    containerHeight: {
      type: String,
      default: '780px'
    },
    containerWidth: {
      type: String,
      default: '100%'
    },
    maxHeight: {
      type: String,
      default: '100vh'
    },
    maxWidth: {
      type: String,
      default: '1600px'
    }
  },
  data () {
    return {
      inputMessage: '',
      messages: [
        {
          sender: 'bot',
          content: '您好！我是Snail系统的AI助手，有什么可以帮您的吗？',
          timestamp: this.getCurrentTime()
        }
      ],
      isLoading: false
    }
  },
  methods: {
    async sendMessage () {
      if (!this.inputMessage.trim() || this.isLoading) return

      const userMessage = {
        sender: 'user',
        content: this.inputMessage,
        timestamp: this.getCurrentTime()
      }

      this.messages.push(userMessage)

      const userInput = this.inputMessage
      this.inputMessage = ''

      this.isLoading = true

      try {
        // 调用API
        const response = await chat({ query: userInput, filePath: '' })

        const botMessage = {
          sender: 'bot',
          content: response.content,
          timestamp: this.getCurrentTime()
        }

        this.messages.push(botMessage)
      } catch (error) {
        console.error('Error:', error)
        this.messages.push({
          sender: 'bot',
          content: '<p>抱歉，处理您的请求时出现了问题。请稍后再试。</p>',
          timestamp: this.getCurrentTime()
        })
      } finally {
        this.isLoading = false
        this.scrollToBottom()
      }
    },

    getCurrentTime () {
      const now = new Date()
      return `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`
    },

    scrollToBottom () {
      this.$nextTick(() => {
        const container = this.$el.querySelector('.chat-history')
        if (container) {
          container.scrollTop = container.scrollHeight
        }
      })
    }
  },
  mounted () {
    this.scrollToBottom()
  }
}
</script>

<style lang="less" scoped>
@import "~ant-design-vue/dist/antd.less";

.chat-container {
  display: flex;
  flex-direction: column;
  margin: 0 auto;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.1);

  .chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 16px;
  }

  .chat-history {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 16px;

    .message {
      display: flex;
      gap: 12px;
      animation: fadeIn 0.3s ease-out;
    }

    .user-message {
      flex-direction: row-reverse;
    }

    .avatar {
      width: 32px;
      height: 32px;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;
      font-size: 14px;
      color: white;
    }

    .user-avatar {
      background: linear-gradient(135deg, @primary-color 0%, #8e2de2 100%);
    }

    .bot-avatar {
      background: linear-gradient(135deg, #00c6ff 0%, #0072ff 100%);
    }

    .message-content {
      max-width: 80%;
      padding: 12px 16px;
      border-radius: 10px;
      line-height: 1.5;
      font-size: 14px;
      position: relative;

      pre {
        background: rgba(0, 0, 0, 0.3);
        border-radius: 6px;
        padding: 10px;
        overflow-x: auto;
        margin: 10px 0;
        font-size: 13px;
        color: #e6e6e6;
      }

      code {
        font-family: 'Fira Code', monospace;
      }
    }

    .user-message .message-content {
      background: rgba(74, 90, 126, 0.5);
      border-bottom-right-radius: 4px;
    }

    .bot-message .message-content {
      background: rgba(30, 41, 59, 0.8);
      border: 1px solid rgba(255, 255, 255, 0.08);
      border-bottom-left-radius: 4px;
    }

    .timestamp {
      font-size: 11px;
      color: rgba(255, 255, 255, 0.4);
      margin-top: 4px;
      text-align: right;
    }

    .loading-dots {
      display: flex;
      gap: 6px;
      padding: 12px;
    }

    .dot {
      width: 6px;
      height: 6px;
      background: @primary-color;
      border-radius: 50%;
      animation: pulse 1.5s infinite ease-in-out;
    }

    .dot:nth-child(2) {
      animation-delay: 0.2s;
    }

    .dot:nth-child(3) {
      animation-delay: 0.4s;
    }
  }

  .input-area {
    padding: 12px 16px;
    background: rgba(15, 23, 42, 0.8);
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    display: flex;
    gap: 10px;
    align-items: center;

    /deep/ .ant-input {
      background: rgba(30, 41, 59, 0.8);
      border: 1px solid rgba(74, 90, 126, 0.5);
      color: #e6e6e6;
      padding: 10px 14px;
      border-radius: 10px;
      font-size: 14px;
      height: auto;
      transition: all 0.3s ease;
      flex: 1;

      &:focus {
        border-color: @primary-color;
        box-shadow: 0 0 0 2px fade(@primary-color, 20%);
      }

      &:hover {
        border-color: @primary-color;
      }
    }

    .send-button {
      background: linear-gradient(135deg, #8e2de2 0%, @primary-color 100%);
      border: none;
      width: 42px;
      height: 42px;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      transition: transform 0.2s ease;
      box-shadow: 0 4px 15px rgba(142, 45, 226, 0.3);

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 20px rgba(142, 45, 226, 0.4);
      }

      &:active {
        transform: translateY(1px);
      }

      i {
        font-size: 16px;
        color: white;
      }
    }
  }
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes pulse {
  0%, 60%, 100% { transform: scale(1); opacity: 1; }
  30% { transform: scale(1.5); opacity: 0.7; }
}

@media (max-width: 768px) {
  .chat-container {
    border-radius: 0;
  }

  .chat-history {
    padding: 8px !important;
    gap: 12px !important;

    .message-content {
      max-width: 85% !important;
      padding: 10px 12px !important;
    }
  }

  .input-area {
    padding: 10px 12px !important;
  }
}
</style>
